/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.losalpes.client;

import com.losalpes.cliente.IClientServices;
import com.losalpes.persistence.entity.Client;
import com.losalpes.persistence.entity.TipoDocumento;
import com.losalpes.security.SecurityBean;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Zully
 */

public class ClientsBean {

    /** Creates a new instance of ClientsBean */
    @EJB
    private IClientServices clientServices;
    private Client client = new Client();
    private String id;

    public ClientsBean(){       
    }

    @PostConstruct
    public void inicializarCliente(){
       FacesContext fc = FacesContext.getCurrentInstance();
	   SecurityBean securityBean = (SecurityBean)fc.getApplication().getELResolver().getValue(fc.getELContext(), null, "SecurityBean");
       client = clientServices.findCliente(securityBean.getUsername());
    }

    public String getId() {
        return id;
    }

    public String findById() {
        client = clientServices.findCliente(id);
        return null;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List getClients() {
        return clientServices.findAll();
    }

    public String add() {
        clientServices.add(client);
        client = new Client();
        return null;
    }

    public String update()
    {
        clientServices.update(client);
        return null;
    }

    public SelectItem[] getTiposDocumento() {
        TipoDocumento[] tipos=  TipoDocumento.values();
        SelectItem[] sitems = new SelectItem[tipos.length];
        for (int i = 0; i < sitems.length; i++) {
             sitems[i] = new SelectItem(tipos[i]);
        }
        return sitems;
    }

    public void delete()
    {
        clientServices.delete(client);
        client = new Client();
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client cClient) {
        this.client = cClient;
    }
}
